package com.gusto.engine.colfil.transformation.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.gusto.engine.colfil.Evaluation;
import com.gusto.engine.colfil.transformation.Transformation;

/**
 * <p>This transformation aims to boost the rare {@link com.gusto.model.Item}s.
 * As opposed to other well-known resources (widely evaluated), 
 * these items need to be promoted an recommended so they can be discovered.</p>
 * <p>In addition to that, the rare items are more useful to similarity computation 
 * than common ones.<br/>
 * More details in <a href="ftp://ftp.research.microsoft.com/pub/tr/tr-98-12.pdf">
 * Empirical Analysis of Predictive Algorithms for Collaborative Filtering</a>.</p>
 * 
 * @author amokrane.belloui@gmail.com
 * 
 */
public class InverseUserFrequencyTransformation implements Transformation {
	
	private Logger log = Logger.getLogger(getClass());
	
	private double logBase;
	/*
	private Model model;
	public void setModel(Model model) throws GustoException {
		this.model = model;
	}
	*/
	public InverseUserFrequencyTransformation(double logBase) {
		this.logBase = logBase;
	}
	
	public List<Evaluation> transform(List<Evaluation> evals) {
		log.info("Applying Inverse User Frequency Transformation");
		
		final double logFactor = Math.log(logBase);
		
		List<Evaluation> res = new ArrayList<Evaluation>(); 
		for (Evaluation e : evals) {
			// TODO Get Inverse Frequency Data from cached Database
			
			//double countEvalItem = model.getEvaluationCountByItem(e.getItemId());
			double countEvalItem = 2;
			//double countUsers = model.getUserCount();
			double countUsers = 300;
			
			double factor = Math.log((countUsers / countEvalItem) / logFactor);
			double result = e.getValue() * factor;
			double norm = result;
			
			e.setValue(norm);
			res.add(e);
		}
		return res;
	}
	
}
